package org.sharing.design.strategy.onStrategy.impl;

import org.sharing.design.strategy.onStrategy.ICouponDiscount;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author: Dragon
 * @since: 2024/4/6 16:05
 */
public class MJCouponDiscount implements ICouponDiscount<Map<String, String>> {

    /**
     * 满减策略
     * 满X元-N元，不满X元不减
     * @param couponInfo
     * @param price
     * @return
     */
    @Override
    public BigDecimal discountAmount(Map<String, String> couponInfo, BigDecimal price) {
        String x = couponInfo.get("x");
        String o = couponInfo.get("n");

        // 小于商品金额条件的，直接返回商品原价
        if (price.compareTo(new BigDecimal(x)) < 0) return price;
        // 减去优惠金额判断
        BigDecimal discountAmount = price.subtract(new BigDecimal(o));
        if (discountAmount.compareTo(BigDecimal.ZERO) < 1) return BigDecimal.ONE;

        return discountAmount;
    }
}
